package de.homelabs.hlmodules.cms.content.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.homelabs.hlmodules.cms.configuration.service.HLConfigurationManager;
import de.homelabs.hlmodules.cms.content.domain.HLPageElement;
import de.homelabs.hlmodules.cms.template.domain.HLImportStrategyException;

@Service("hlPageManager")
public class HLPageManager {

	@Autowired
	private HLConfigurationManager configManager;
	
	private Map<String, String> pageList = new HashMap<String, String>();
	
	/**
	 * private constructor
	 */
	public HLPageManager(){
	}
	
	public Map<String, Object> getContentByRequest(String request, String domain) throws IOException{
		StringBuffer content = new StringBuffer();
		String fname = "";
		String pageTitle = "";
		
		//first import page list
		importPageList(domain);
		
		//if request is index.htm or / then we have the first page
		if (request.contains("index.htm")){
			fname = (String) this.pageList.keySet().toArray()[0];
			pageTitle = (String) this.pageList.values().toArray()[0];
		} else {
			String tmp = request.replaceAll(".htm", "");
			tmp = tmp.replaceAll("/", "");
			for (String key : pageList.keySet()){
				if (pageList.get(key).equals(tmp)){
					fname = key;
					pageTitle = tmp;
				}
			}
		}
		//load file
		File file = new File(configManager.getConfigurationByDomain(domain).getConfigPath() + "//" + domain + "//pages//"+fname);
		if (file.exists()){
			
			//read content
			byte[] buffer = new byte[ (int) file.length() ]; 
			InputStream in = new FileInputStream( file ); 
			in.read(buffer);
			in.close();
			content.append(new String(buffer));
		}
		
		//build result
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("content", content);
		model.put("pageTitle", pageTitle);
		
		//return
		return model;
	}
	
	/********************
	 * functions
	 * @throws HLImportStrategyException 
	 */
	public HLPageElement getPageElementByURL(String url) throws HLImportStrategyException{
//		//load content
//		importPageList();
//		
//		//find page
//		for (HLPageElement pageElement : this.pageList.getPages()){
//			if (pageElement.getPageUrl().equalsIgnoreCase(url)){
//				//return element
//				return pageElement;
//			}
//		}
//		
//		//no Element found
//		return null;
		return null;
	}
	
	private boolean importPageList(String domain){
		//Page Directory
		File file = new File(configManager.getConfigurationByDomain(domain).getConfigPath() + "//" + domain + "//pages");
		List<String> files = Arrays.asList(file.list());
		if (files.size() > 0) {
			//clear actual pageList if previously filled
			pageList.clear();
			
			//sort files
			Collections.sort(files);
			
			//loop
			for (String page : files){
				//convert filenames
				//TODO: define convertig rules in config/propertie file for user customizing
				String convertedPage = page.replaceAll(".txt", "");
				convertedPage = convertedPage.replaceAll("[0-9]*_", "");
				//add to pagelist
				pageList.put(page, convertedPage);
			}
			return true;
		} else {
			return false;
		}
	}
	
	public Map<String, String> getPageList(){
		return this.pageList;
	}
}
